﻿Imports System.Math

Public Class frmEntryProtocol

    'declarations creation objet observation
    Public malongitude As Nullable(Of Double)
    Public malatitude As Nullable(Of Double)
    Public monaltitude As Nullable(Of Double)
    Public madop As Nullable(Of Double)
    Public monEspece As String
    Public monIdEspece As Nullable(Of Integer)
    Public maprecision As Nullable(Of Double)
    Public madescription As String
    Public monIdZoneEtude As Integer ' As Nullable(Of Integer)
    Public maZoneEtude As String
    Public mawptname As String = "Null"
    Public maStation As ClsStation
    Public monObservation As ClsObservation
    Public maIndexStation As Nullable(Of Integer)
    Private maIdObs As Integer
    Public oObs As ClsObservation
    Public Quality As String = "Null"
    Public lastSelectedPr As String

    ' variables propres a l'affichage de champs de protocole
    Public btnOrder As Short = 1

    Public Tab As New System.Windows.Forms.TabControl
    Public oProtocoleValues As clsProtocolValues
    Dim m_lastform As String



    Private Sub formAffichage_load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated

        Try

            Dim numOnglet As Short
            'Si le dernier protocole chargé était le même alors ==> ne pas changer le formulaire
            If lastSelectedPr = frmEntryChoixProtocol.selectedProtocolId Then
                'Remise a zero des valeurs du formulaire
                Dim prev As String = previous
                'If Not frmDataObsTable.previous = "frmDataChoix" Or Not frmDataObsTable.previous = "frmEntryFin" Then
                If IsNothing(oProtocoleValues) Then
                    resetFieldValue()
                    If Tab.TabPages.Count > 0 Then Tab.SelectedIndex = 0
                End If
                Exit Sub
                'End If
            End If

            '1 : suppression de tous les controles du formulaire
            Me.initProtocolFieldControl()
            'Recuperation du protocole selectionné
            '1 : id
            '2 : object protocole
            Dim oActualProtocol As clsProtocol = DirectCast(frmEntryChoixProtocol.olistProtocoles.Protocols(frmEntryChoixProtocol.selectedProtocolId), clsProtocol)

            'nb de champ à créer
            Dim nbFields As Integer
            nbFields = oActualProtocol.ProtocolFieldsNumber
            lastSelectedPr = frmEntryChoixProtocol.selectedProtocolId
            'Si 2 contoles pas de controle tab

            Dim field As Object
            Dim type As String
            Dim value As String = ""
            field = oActualProtocol.fields.fieldsCollection.Item(0)
            type = field.GetType.Name.ToString

            If ((nbFields = 1) And (type = "clsFieldPhoto")) Then

                '  If type = "clsFieldPhoto" Then
                Dim fieldPhoto As clsFieldPhoto = CType(field, clsFieldPhoto)
                ajoutControlsChampsTypePhoto(fieldPhoto.fieldId, fieldPhoto.FieldDisplayLabel)
                ' End If

            Else
                ' creer un controle tab 

                Dim nbOnglets As Integer = createTabControl(nbFields)

                'parcourir la liste de champs et creer les controles en fonction de leur type

                ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                ' Dim field As Object
                '  Dim type As String
                Dim btnOrder As Integer = 1
                '   Dim value As String = ""
                Dim btnPositionInTab As Short
                For Each field In (oActualProtocol.fields.fieldsCollection)
                    type = field.GetType.Name.ToString
                    Select Case type
                        Case "clsFieldNumeric"
                            Dim fieldNumeric As clsFieldNumeric = CType(field, clsFieldNumeric)
                            numOnglet = controlPosition(btnOrder)
                            ' connaitre la position dans l'onglet
                            btnPositionInTab = positionInTab(btnOrder)
                            Dim defaultValue As String
                            If IsNothing(fieldNumeric.default_value) Then
                                defaultValue = ""
                            Else
                                defaultValue = fieldNumeric.default_value.ToString
                            End If
                            'Récupération de la valeur courrante du controle
                            value = getCurrentValueForCtl(fieldNumeric.FieldDisplayLabel, defaultValue)
                            ajoutControlsChampsTypeEntier(fieldNumeric.fieldId, fieldNumeric.FieldDisplayLabel, fieldNumeric.unit, numOnglet, btnPositionInTab, btnOrder, _
                                                          fieldNumeric.maxBound, fieldNumeric.minBound, value, defaultValue)
                            btnOrder = btnOrder + 1
                        Case "clsFieldBoolean"
                            Dim fieldBoolean As clsFieldBoolean = CType(field, clsFieldBoolean)
                            numOnglet = controlPosition(btnOrder)
                            ' connaitre la position dans l'onglet
                            btnPositionInTab = positionInTab(btnOrder)
                            'Récupération de la valeur courrante du controle
                            value = getCurrentValueForCtl(fieldBoolean.FieldDisplayLabel, fieldBoolean.default_value)
                            ajoutControlsChampsTypeBool(fieldBoolean.fieldId, fieldBoolean.FieldDisplayLabel, numOnglet, btnPositionInTab, btnOrder, value)
                            btnOrder = btnOrder + 1
                        Case "clsFieldDate"
                            Dim fieldlDate As clsFieldDate = CType(field, clsFieldDate)
                            ' connaite l'onglet dans lequel il sera inseré
                            numOnglet = controlPosition(btnOrder)
                            ' connaitre la position dans l'onglet
                            btnPositionInTab = positionInTab(btnOrder)
                            'Récupération de la valeur courrante du controle
                            value = getCurrentValueForCtl(fieldlDate.FieldDisplayLabel, fieldlDate.default_value)
                            ajoutControlsChampsTypeDate(fieldlDate.fieldId, fieldlDate.FieldDisplayLabel, numOnglet, btnPositionInTab, btnOrder, value)
                            btnOrder = btnOrder + 1
                        Case "clsFieldText"
                            Dim fieldlText As clsFieldText = CType(field, clsFieldText)
                            ' connaite l'onglet dans lequel il sera inseré
                            numOnglet = controlPosition(btnOrder)
                            ' connaitre la position dans l'onglet
                            btnPositionInTab = positionInTab(btnOrder)
                            'Récupération de la valeur courrante du controle
                            value = getCurrentValueForCtl(fieldlText.FieldDisplayLabel, fieldlText.default_value)
                            ajoutControlsChampsTypeTexte(fieldlText.fieldId, fieldlText.FieldDisplayLabel, numOnglet, btnPositionInTab, btnOrder, value)
                            btnOrder = btnOrder + 1
                        Case "clsFieldList"
                            Dim fieldlist As clsFieldList
                            Dim listeVal As String
                            Dim nbItems As Short
                            Dim i As Short
                            fieldlist = CType(field, clsFieldList)
                            numOnglet = controlPosition(btnOrder)
                            ' connaitre la position dans l'onglet
                            btnPositionInTab = positionInTab(btnOrder)
                            ' recuperer les labels des items
                            nbItems = fieldlist.items.Count
                            If nbItems > 0 Then
                                listeVal = fieldlist.items.Item(0).label
                                For i = 1 To nbItems - 1
                                    listeVal = listeVal & ";" & fieldlist.items.Item(i).label
                                Next
                            Else
                                listeVal = ""
                            End If
                            'Récupération de la valeur courrante du controle
                            value = getCurrentValueForCtl(fieldlist.FieldDisplayLabel, fieldlist.defaultItem.label)
                            ajoutControlsChampsTypeListe(fieldlist.fieldId, fieldlist.FieldDisplayLabel, listeVal, numOnglet, btnPositionInTab, _
                                                         btnOrder, value)
                            btnOrder = btnOrder + 1

                        Case "clsFieldGroup"
                            Dim fieldgroup As clsFieldGroup
                            Dim listeVal1, listeVal2 As String
                            Dim nbItems1, nbItems2 As Short
                            Dim i As Short

                            fieldgroup = CType(field, clsFieldGroup)
                            numOnglet = controlPosition(btnOrder)
                            ' connaitre la position dans l'onglet
                            btnPositionInTab = positionInTab(btnOrder)

                            ' recuperer les labels des items de liste1
                            nbItems1 = fieldgroup.items1.Count
                            If nbItems1 > 0 Then
                                listeVal1 = fieldgroup.items1.Item(0).label
                                For i = 1 To nbItems1 - 1
                                    listeVal1 = listeVal1 & ";" & fieldgroup.items1.Item(i).label
                                Next
                            Else
                                listeVal1 = ""
                            End If
                            ' recuperer les labels des items de liste2
                            nbItems2 = fieldgroup.items2.Count
                            If nbItems1 > 0 Then
                                listeVal2 = fieldgroup.items2.Item(0).label
                                For i = 1 To nbItems2 - 1
                                    listeVal2 = listeVal2 & ";" & fieldgroup.items2.Item(i).label
                                Next
                            Else
                                listeVal2 = ""
                            End If
                            'Récupération de la valeur courrante du controle
                            value = getCurrentValueForCtl(fieldgroup.FieldDisplayLabel, fieldgroup.default_item1.label & " " & fieldgroup.default_item2.label)
                            ajoutControlsChampsTypeDeuxListes(fieldgroup.fieldId, fieldgroup.FieldDisplayLabel, listeVal1, listeVal2, numOnglet, _
                                                              btnPositionInTab, btnOrder, value)
                            btnOrder = btnOrder + 1

                    End Select
                Next
            End If
        Catch ex As Exception
            MsgBox("Form load error")
        End Try
    End Sub
#Region "Creation tab control"

    Public Sub initProtocolFieldControl()
        Dim k As Integer = 0

        'Nétoyage du formulaire. dans ce cas de figure le controle tab n'est pas supprimé, à revoir si on a un protocole avec moins de 2 champs, pas de controle tab
        For k = 0 To (Tab.TabPages.Count - 1)
            For Each ctl As Control In Tab.Controls.Item(k).Controls
                Tab.Controls.Remove(ctl)
            Next
        Next

        k = Tab.TabPages.Count - 1

        While (k > 0 Or k = 0)
            Tab.TabPages.RemoveAt(k)
            k = k - 1
        End While
        'For Each ctl As Control In Tab.Controls
        '    Me.Controls.Remove(ctl)
        'Next
        Me.Controls.Remove(Tab)
        ' verifier l'existence du controle photo et le supprimer
        For Each ctl As Control In Controls
            Dim ctlName As String
            ctlName = ctl.Name
            If ctlName.Substring(0, 3) = "Pho" Then
                Controls.Remove(ctl)
            End If
        Next
        ' reinitialiser la variable servant de stocker d'ordre des controles
        btnOrder = 1
    End Sub

    Function createTabControl(ByVal nbFields As Short) As Short
        Dim nbOnglets As Integer
        Dim i As Integer

        Tab.Size = New Size(240, 224)
        Tab.Anchor = AnchorStyles.None
        Tab.Location = New Point(0, 22)
        ' claculer le nombre d'onglets necessaires
        nbOnglets = controlPosition(nbFields)

        For i = 1 To nbOnglets
            ' nbOngletsInt()
            Dim page As System.Windows.Forms.TabPage
            page = New System.Windows.Forms.TabPage
            page.Text = "p_" & i
            page.Name = "page " & i
            Tab.Controls.Add(page)
        Next
        Controls.Add(Tab)
        Return nbOnglets
    End Function

    Function controlPosition(ByVal order As Short) As Short
        Dim decimalOrder As Double
        Dim orderInt As Short

        decimalOrder = order / 2
        If CInt(decimalOrder) < decimalOrder Then
            orderInt = CInt(decimalOrder) + 1
        Else
            orderInt = CInt(decimalOrder)
        End If
        'If orderInt = 0 Then orderInt = 1

        Return orderInt
    End Function

    Function positionInTab(ByVal order As Short) As Short
        ' si l'ordre du champ par rappo rt au nombre total des champs est un nombre pair il occupe la premiere position dans l'onglet sinon la deuxième
        Dim DecOrder As Double

        DecOrder = order
        If (CInt(DecOrder / 2) * 2) - DecOrder = 0 Then
            Return 2
        Else : Return 1
        End If
    End Function
#End Region

#Region "Ajout Controles"

    Public Sub ajoutControlsChampsTypeEntier(ByVal intIdVariable As Integer, ByVal strNomVariable As String, ByVal strUnit As String, ByVal numOnglet As Short, _
                    ByVal fieldPosition As Short, ByVal numControl As Short, ByVal max As Nullable(Of Integer), ByVal min As Nullable(Of Integer), ByVal defautvalue As String, Optional ByVal value As String = "")
        Try

            Dim yLoc As Short
            Dim ctlField As CtlFieldInteger

            ctlField = New CtlFieldInteger
            ctlField.max = max
            ctlField.min = min

            'Positionnement du champ en fonction de son ordre
            If fieldPosition = 1 Then
                yLoc = 10
            Else
                yLoc = 105
            End If
            If (strNomVariable <> "Null") Then

                'Positionnement du control
                ctlField.Name = "numProt" & intIdVariable
                ctlField.Location = New Point(11, yLoc)
                ctlField.Anchor = AnchorStyles.Top
                'Contenu
                ctlField.TxtFieldName.Text = strNomVariable
                ctlField.txtIdField.Text = intIdVariable.ToString
                ctlField.defaultValue = defautvalue
                If ((strUnit <> "") And (strUnit <> "Null")) Then
                    ctlField.TxtValUnit.Text = strUnit
                End If

                'Ajout du control

                '   Controls.Add(ctlField)
                Tab.Controls.Item(numOnglet - 1).Controls.Add(ctlField)
            End If

            'Valeur
            If (value <> "") Then
                ctlField.TxtValue.Text = value
            End If
        Catch ex As Exception
            MsgBox("erreur dans le type de variable")
        End Try
    End Sub

    Private Sub ajoutControlsChampsTypeBool(ByVal intIdVariable As Integer, ByVal strNomVariable As String, ByVal numOnglet As Short, ByVal fieldPosition As Short, ByVal numControl As Short, Optional ByVal value As String = "")

        Dim yLoc As Short

        'Positionnement du champ en fonction de son ordre
        If fieldPosition = 1 Then
            yLoc = 10
        Else
            yLoc = 105
        End If

        Dim ctlField As CtlFieldBoolean

        ctlField = New CtlFieldBoolean

        If strNomVariable <> "Null" Then

            'Positionnement du control
            ctlField.Name = "bolProt" & strNomVariable
            ctlField.Location = New Point(11, yLoc)
            ctlField.Anchor = AnchorStyles.Top
            'Contenu
            ctlField.TxtFieldName.Text = strNomVariable
            ctlField.txtIdField.Text = intIdVariable.ToString
            'valeur
            If (value <> "") Then
                If (value = "True") Then
                    ctlField.ctlvalue = True
                    '   ctlField.BtnTrue.Focus()
                Else
                    ctlField.ctlvalue = False
                    ' ctlField.BtnFalse.Focus()
                End If
            End If

            'Ajout du control
            'Controls.Add(ctlField)
            Tab.Controls.Item(numOnglet - 1).Controls.Add(ctlField)
        End If
    End Sub

    Private Sub ajoutControlsChampsTypeTexte(ByVal intIdVariable As Integer, ByVal strNomVariable As String, ByVal numOnglet As Short, ByVal fieldPosition As Short, ByVal numControl As Short, ByVal defaultvalue As String, Optional ByVal value As String = "")
        Dim yLoc As Short

        'Positionnement du champ en fonction de son ordre
        If fieldPosition = 1 Then
            yLoc = 10
        Else
            yLoc = 105
        End If
        Dim ctlField As CtlFieldText

        ctlField = New CtlFieldText

        If strNomVariable <> "Null" Then

            'Positionnement du control
            ctlField.Name = "txtProtValeurVar" & numControl
            ctlField.Location = New Point(11, yLoc)
            ctlField.Anchor = AnchorStyles.Top
            ctlField.SendToBack()
            'Contenu
            ctlField.TxtFieldName.Text = strNomVariable
            ctlField.txtIdField.Text = intIdVariable.ToString
            ctlField.defaultValue = defaultvalue
            'valeur
            If (value <> "") Then
                ctlField.TxtValue.Text = value
            End If

            'Ajout du control
            ' Controls.Add(ctlField)
            Tab.Controls.Item(numOnglet - 1).Controls.Add(ctlField)
        End If
    End Sub

    Sub ajoutControlsChampsTypeListe(ByVal intIdVariable As Integer, ByVal strNomVariable As String, ByVal listeValeurs As String, ByVal numOnglet As Short, ByVal fieldPosition As Short, ByVal numControl As Short, Optional ByVal value As String = "")
        Dim yLoc As Short
        Dim ctlField As CtlFieldList
        ctlField = New CtlFieldList

        'Positionnement du champ en fonction de son ordre
        If fieldPosition = 1 Then
            yLoc = 10
        Else
            yLoc = 105
        End If

        If strNomVariable <> "Null" Then

            'Positionnement du control
            ctlField.Name = "lstProtValeurVar" & numControl
            ctlField.Location = New Point(11, yLoc)
            ctlField.Anchor = AnchorStyles.Top

            'Contenu
            ctlField.TxtFieldName.Text = strNomVariable
            ctlField.txtIdField.Text = intIdVariable.ToString
            'Initialisation de la liste du combobox
            'Start index = début de la liste de valeur actuellement = 2
            ctlField.insertComboboxValue(listeValeurs, 0, value)

            'Ajout du control
            Tab.Controls.Item(numOnglet - 1).Controls.Add(ctlField)
        End If
    End Sub

    Sub ajoutControlsChampsTypePhoto(ByVal intIdVariable As Integer, ByVal strNomVariable As String)
        Dim yLoc As Short
        Dim ctlField As CtlFieldPhoto
        ctlField = New CtlFieldPhoto

        'Positionnement du champ en fonction de son ordre

        yLoc = 30

        If strNomVariable <> "Null" Then

            'Positionnement du control
            ctlField.Name = "PhoProtValeurVar"
            ctlField.Location = New Point(11, yLoc)
            ctlField.Anchor = AnchorStyles.Top

            'Contenu
            ctlField.PhotoFieldName.Text = strNomVariable
            ctlField.txtIdField.Text = intIdVariable.ToString
            Me.Controls.Add(ctlField)
        End If
    End Sub

    Sub ajoutControlsChampsTypeDeuxListes(ByVal intIdVariable As Integer, ByVal strNomVariable As String, ByVal listeValeurs1 As String, ByVal listeValeurs2 As String, ByVal numOnglet As Short, ByVal fieldPosition As Short, ByVal numControl As Short, Optional ByVal value As String = "")
        Dim yLoc As Short
        Dim ctlField As CtlField2Lists2
        ctlField = New CtlField2Lists2

        'Positionnement du champ en fonction de son ordre
        If fieldPosition = 1 Then
            yLoc = 10
        Else
            yLoc = 105
        End If

        If strNomVariable <> "Null" Then

            'Positionnement du control
            ctlField.Name = "ls2ProtValeurVar" & numControl
            ctlField.Location = New Point(11, yLoc)
            ctlField.Anchor = AnchorStyles.Top

            'Contenu
            ctlField.TxtFieldName.Text = strNomVariable
            'Initialisation de la liste du combobox
            'Start index = début de la liste de valeur actuellement = 2
            ctlField.insertComboboxValue(listeValeurs1, listeValeurs2, value)
            ctlField.txtIdField.Text = intIdVariable.ToString

            'Ajout du control
            'Controls.Add(ctlField)
            Tab.Controls.Item(numOnglet - 1).Controls.Add(ctlField)
        End If
    End Sub

    Private Sub ajoutControlsChampsTypeDate(ByVal intIdVariable As Integer, ByVal strNomVariable As String, ByVal numOnglet As Short, ByVal fieldPosition As Short, ByVal numControl As Short, Optional ByVal value As String = "")
        Dim yLoc As Short
        'Positionnement du champ en fonction de son ordre
        If fieldPosition = 1 Then
            yLoc = 10
        Else
            yLoc = 105
        End If
        Dim ctlField As CtlFieldTime
        ctlField = New CtlFieldTime

        If strNomVariable <> "Null" Then
            'Positionnement du control
            ctlField.Name = "datProtValeurVar" & numControl
            ctlField.Location = New Point(11, yLoc)
            ctlField.Anchor = AnchorStyles.Top
            ctlField.SendToBack()
            'Contenu
            ctlField.TxtFieldName.Text = strNomVariable
            ctlField.txtIdField.Text = intIdVariable.ToString
            'valeur
            If (value <> "") Then
                ctlField.TxtTimeValue.Text = value
            End If
            'Ajout du control
            ' Controls.Add(ctlField)
            Tab.Controls.Item(numOnglet - 1).Controls.Add(ctlField)
        End If
    End Sub

#End Region


    Private Function returnCtlValue(ByVal ctl As Control) As String
        Dim strValue As String = "Null"
        Dim ctlName, strType As String

        If (Not IsNothing(ctl)) Then

            ctlName = ctl.Name
            strType = ctlName.Substring(0, 3)

            Select Case strType
                Case "num"
                    Dim ctlField As CtlFieldInteger
                    ctlField = DirectCast(ctl, CtlFieldInteger)
                    strValue = ctlField.TxtValue.Text & ";" & ctlField.TxtFieldName.Text & ";" & ctlField.txtIdField.Text
                Case "bol"
                    Dim ctlField As CtlFieldBoolean
                    ctlField = DirectCast(ctl, CtlFieldBoolean)
                    strValue = ctlField.ctlvalue.ToString & ";" & ctlField.TxtFieldName.Text & ";" & ctlField.txtIdField.Text
                Case "txt"
                    Dim ctlField As CtlFieldText
                    ctlField = DirectCast(ctl, CtlFieldText)
                    ' formater la chaine : supprimer le retour chariot s'il existe + espaces au debut et à la fin de la chaine
                    Dim strValText As String
                    strValText = ctlField.TxtValue.Text
                    If strValText <> "" Then
                        strValText = strValText.Replace(vbCrLf, " ")
                        strValText = strValText.Trim
                    End If
                    strValue = strValText & ";" & ctlField.TxtFieldName.Text & ";" & ctlField.txtIdField.Text
                Case "dat"
                    Dim ctlField As CtlFieldTime
                    ctlField = DirectCast(ctl, CtlFieldTime)
                    strValue = ctlField.TxtTimeValue.Text & ";" & ctlField.TxtFieldName.Text & ";" & ctlField.txtIdField.Text
                Case "lst"
                    Dim ctlField As CtlFieldList
                    ctlField = DirectCast(ctl, CtlFieldList)
                    If ((ctlField.CmbValue.SelectedItem <> "")) Then
                        strValue = ctlField.CmbValue.SelectedItem.ToString & ";" & ctlField.TxtFieldName.Text & ";" & ctlField.txtIdField.Text
                    Else
                        strValue = "Null" & ";" & ctlField.TxtFieldName.Text & ";" & ctlField.txtIdField.Text & ";" & ctlField.txtIdField.Text
                    End If
                Case "ls2"
                    Dim ctlField As CtlField2Lists2
                    ctlField = DirectCast(ctl, CtlField2Lists2)
                    If ((ctlField.CmbValue1.SelectedItem <> "")) And ((ctlField.CmbValue2.SelectedItem <> "")) Then
                        strValue = ctlField.CmbValue1.SelectedItem.ToString & " " & ctlField.CmbValue2.SelectedItem.ToString & ";" & ctlField.TxtFieldName.Text & ";" & ctlField.txtIdField.Text
                    Else
                        strValue = "Null" & ";" & ctlField.TxtFieldName.Text & ";" & ctlField.txtIdField.Text
                    End If
            End Select
        End If
        Return strValue
    End Function
#Region "Evènements"
    Private Sub BtnSuivant_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSuivant.Click
        Dim test2 As String = ""

        Try
            If frmEntryChoixProtocol.selectedProtocolId = "Photo" Then
                frmEntryChoixProtocol.Show()
                Me.Hide()
            Else



                'Initialisation de l'object : valeur des champs du protocole en cours de saisie
                instanciateFieldValueObject()

                If frmEntryChoixProtocol.multiSpecies = True Then
                    ' creer l'objet observation pour sauvegarde des valeurs saisies pendant la séquence
                    oObs = New ClsObservation(monIdEspece, monEspece, Quality, frmEntryChoixTaxon.strNomPhoto, oProtocoleValues.fieldsValues)
                    'afficher le bouton nouveau taxon s'il etait masqué
                    frmEntryFin.btnNvEspèce.Visible = True
                Else

                    oObs = New ClsObservation(oProtocoleValues.fieldsValues)
                    'masquer le bouton nouveau taxon
                    frmEntryFin.btnNvEspèce.Visible = False
                End If

                If (Not IsNothing(maIndexStation)) Then
                    DirectCast(oProtocolsCurrentListObs(frmEntryChoixProtocol.selectedProtocolId), ClsListObservations).lstStations().Item(CInt(maIndexStation)).AddOneObjectObservation(oObs)
                Else
                    maStation = New ClsStation(System.DateTime.Now, _
                                               mawptname, malongitude, malatitude, monaltitude, madop, maprecision, _
                                               monIdZoneEtude, maZoneEtude, madescription, _
                                               frmParamObservateur.intIdObs1, frmParamObservateur.CmbObs1.Text, frmParamObservateur.intIdObs2, _
                                               frmParamObservateur.CmbObs2.Text, 1)
                    maStation.AddOneObjectObservation(oObs)
                    'Test une données de ce protocole a déjà été saisie
                    'Si ce n'est pas le cas création de l'entré
                    If Not oProtocolsCurrentListObs.ContainsKey(frmEntryChoixProtocol.selectedProtocolId) Then
                        oProtocolsCurrentListObs.Add(frmEntryChoixProtocol.selectedProtocolId, New ClsListObservations)
                    End If
                    maIndexStation = DirectCast(oProtocolsCurrentListObs(frmEntryChoixProtocol.selectedProtocolId), ClsListObservations).AddOneObjectStation(maStation)
                End If

                frmEntryFin.Show()
                Me.Hide()
            End If
        Catch ex As Exception
            MsgBox(DirectCast(My.Resources.Images.errorOnSave, String) & ex.Message, MsgBoxStyle.Critical, "Pocket eRelevé")
        End Try
    End Sub

    Private Sub BtnRetour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRetour.Click
        Try
            'Initialisation de l'object : valeur des champs du protocole en cours de saisie
            instanciateFieldValueObject()

            Select Case m_lastform
                Case "frmEntryGPS"
                    frmEntryGPS.Show()
                Case "frmEntryChoixTaxon"
                    frmEntryChoixTaxon.Show()
                Case "frmEntryChoixSite"
                    frmEntryChoixSite.Show()
            End Select

            Me.Hide()
        Catch ex As Exception
            MsgBox(DirectCast(My.Resources.Images.errorOnSave, String) & ex.Message, MsgBoxStyle.Critical, "Pocket eRelevé")
        End Try
    End Sub

    Private Sub instanciateFieldValueObject()

        Dim strCtlValue As String
        Dim strValue As String
        Dim strFieldName As String
        Dim strFieldId As Short
        Dim i As Integer
        Dim s As Short = 0

        ' creer un objet oProtocoleValues pour stocker nom de protocole + les valeurs de champs
        If (Not IsNothing(oProtocoleValues)) Then
            oProtocoleValues = Nothing
        End If
        oProtocoleValues = New clsProtocolValues

        ' creer objet et netoyer interface de saisie et exporter objet 

        '' parcourir la collection des controles dans le sens inverse pour conserver l'ordre d'affichage des champs
        For i = (Tab.TabPages.Count - 1) To 0 Step -1

            For Each ctl As Control In Tab.Controls.Item(i).Controls

                strCtlValue = returnCtlValue(ctl)
                If strCtlValue <> "Null" Then

                    strValue = strCtlValue.Split(";")(0)
                    strFieldName = strCtlValue.Split(";")(1)
                    strFieldId = CType(strCtlValue.Split(";")(2), Short)
                    ' creer un objet ( nom de champ + valeur )
                    Dim oFieldValue As New clsProtocolFieldValue(strFieldId, strFieldName, strValue)
                    ' ajouter l'objet a la liste 
                    oProtocoleValues.fieldsValues.Add(oFieldValue)
                    s = s + 1
                End If
            Next
        Next
    End Sub

    Private Sub BtnMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnMenu.Click
        FormMain.Show()
        Me.Hide()
    End Sub

    Private Sub Keyboard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Keyboard.Click
        ClSIP.ChangeState()
    End Sub

    Private Sub CtlLightGPSTrack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        frmUtilityTracks.Show()
        Me.Hide()
    End Sub
#End Region

    ' proprieté permettant de connaitre le dernier form affiché pour parametrer la fleche precedent
    Public Property lastform() As String
        Get
            Return m_lastform
        End Get
        Set(ByVal value As String)
            m_lastform = value
        End Set
    End Property

    'Fonction qui détermine la valeur de l'object courrant pour un controle donné
    Private Function getCurrentValueForCtl(ByVal strFieldName As String, ByVal defaultValue As String) As String
        Dim value As String = ""
        If (Not IsNothing(oProtocoleValues)) Then
            Dim i As Integer = 0
            For i = 0 To oProtocoleValues.fieldsValues.Count - 1
                If (oProtocoleValues.fieldsValues(i).fieldName = strFieldName) Then
                    value = oProtocoleValues.fieldsValues(i).fieldValue
                    Exit For
                End If
            Next
        End If

        'Test si pas de valeur déjà saisie alors valeur par défaut
        If ((value = "") And (defaultValue <> "")) Then
            value = defaultValue
        End If
        Return value
    End Function

    Private Sub resetFieldValue()
        '' parcourir la collection des controles dans le sens inverse pour conserver l'ordre d'affichage des champs
        Try
            For i As Integer = (Tab.TabPages.Count - 1) To 0 Step -1
                For Each ctl As Control In Tab.Controls.Item(i).Controls
                    Dim ctlName As String
                    Dim strType As String

                    If (Not IsNothing(ctl)) Then
                        If ctl.Name.Length > 3 Then
                            ctlName = ctl.Name
                            strType = ctlName.Substring(0, 3)

                            Select Case strType
                                Case "num"
                                    Dim ctlField As CtlFieldInteger
                                    ctlField = DirectCast(ctl, CtlFieldInteger)
                                    ctlField.setDefaultvalue()
                                Case "bol"
                                    Dim ctlField As CtlFieldBoolean
                                    ctlField = DirectCast(ctl, CtlFieldBoolean)
                                    ctlField.setDefaultvalue()
                                Case "txt"
                                    Dim ctlField As CtlFieldText
                                    ctlField = DirectCast(ctl, CtlFieldText)
                                    ctlField.setDefaultvalue()
                                Case "dat"
                                    Dim ctlField As CtlFieldTime
                                    ctlField = DirectCast(ctl, CtlFieldTime)
                                    ctlField.setDefaultvalue()
                                Case "lst"
                                    Dim ctlField As CtlFieldList
                                    ctlField = DirectCast(ctl, CtlFieldList)
                                    ctlField.setDefaultvalue()
                                Case "ls2"
                                    Dim ctlField As CtlField2Lists2
                                    ctlField = DirectCast(ctl, CtlField2Lists2)
                                    ctlField.setDefaultvalue()
                            End Select
                        End If
                    End If

                Next
            Next
        Catch ex As Exception
            MsgBox("Error reset : " & ex.Message)
        End Try
    End Sub

    Private Sub frmEntryProtocol_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.GotFocus
        previous = Me.Name
    End Sub
End Class